package org.chnxi.leetcode.problems.ques12;

/**
 * 整数转罗马数字
 * 执行用时 :5 ms, 在所有 Java 提交中击败了88.05%的用户
 * 内存消耗 :38.9 MB, 在所有 Java 提交中击败了11.11%的用户
 */
public class Simple01 {

    public static void main(String[] args) {
        int num1 = 3;
        String result1 = intToRoman(num1);
        System.out.println("num1="+result1);

        int num2 = 4;
        String result2 = intToRoman(num2);
        System.out.println("num2="+result2);

        int num3 = 9;
        String result3 = intToRoman(num3);
        System.out.println("num3="+result3);

        int num4 = 58;
        String result4 = intToRoman(num4);
        System.out.println("num4="+result4);

        int num5 = 1994;
        String result5 = intToRoman(num5);
        System.out.println("num5="+result5);

        int num6 = 20;
        String result6 = intToRoman(num6);
        System.out.println("num6="+result6);
    }

    //定义罗马数字可能出现的情况
    final static String[] arr = {
            "M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"
    };

    //针对arr列举对应的值
    final static int[] nums = {
            1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
    };


    public static String intToRoman(int num) {
        //不能处理小于1的数字
        if(num < 1 || num > 3999){
            return "";
        }

        //记录结果
        StringBuilder result = new StringBuilder("");

        while (num > 0){

            int i =0; //记录罗马数字的下标位
            //循环判断罗马数字对应的值
            while (i < nums.length){

                //判断是否可以取当前下标位上的数字
                if(num >= nums[i]){
                    num = num - nums[i];
                    result.append(arr[i]);
                }

                //剩余数值如果不大于当前罗马数值，去判断下一个
                if(num < nums[i]){
                    i++;
                }
            }
        }

        return result.toString();
    }
}
